clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"

use revealedprefDSBCNoTimePressureBars,clear

*Create the cumulative choice and ratio variables for each subject and pairs
egen sumleft = sum(choiceleft), by(subj idpair)
egen sumright = sum(1-choiceleft), by(subj idpair)
gen ratioA = sumleft/repetition
gen ratioB = sumright/repetition
sum sumleft sumright ratioA ratioB
gen pchosen = ratioB / ratioA if ratioA>= ratioB
replace pchosen = ratioA / ratioB if ratioA<ratioB
tab pchosen

sort RTordered, stable 
* Loop over subjects and pairs to calculate the RTs threshold given the CDF and choice proportion.
 local j = 1
          while `j' < 61{
 local i = 1
          while `i' < 21{
        capture gen RtpredA`i'  = .
        capture confirm variable cdfAsubj`j'id`i'
        if !_rc {
            sort cdfAsubj`j'id`i'
			sum pchosen if subj == `j' & idpair == `i'
            gen LowerBoundsubj`j'id`i'=(cdfAsubj`j'id`i'<=r(mean))
			sum cdfAsubj`j'id`i', detail
			replace LowerBoundsubj`j'id`i'=1 if cdfAsubj`j'id`i'== r(min)
            gen UpperBoundsubj`j'id`i'=(cdfAsubj`j'id`i'>r(mean)) 
			sum cdfAsubj`j'id`i', detail
			replace UpperBoundsubj`j'id`i'=1 if cdfAsubj`j'id`i'==r(max)
            gen TresholdsubLowerj`j'id`i'=(LowerBoundsubj`j'id`i'+LowerBoundsubj`j'id`i'[_n-1])
            gen TresholdsubUpperj`j'id`i'=(UpperBoundsubj`j'id`i'+UpperBoundsubj`j'id`i'[_n+1])
            sum RTordered if TresholdsubLowerj`j'id`i'==1 | TresholdsubUpperj`j'id`i'==1, detail
            replace RtpredA`i'  = r(mean) if subj==`j'
           drop LowerBoundsubj`j'id`i' UpperBoundsubj`j'id`i' TresholdsubLowerj`j'id`i' TresholdsubUpperj`j'id`i'
		   }
        capture gen RtpredB`i'  = .
        capture confirm variable cdfBsubj`j'id`i'
        if !_rc {
            sort cdfBsubj`j'id`i'
			sum pchosen if subj == `j' & idpair == `i'
            gen LowerBoundsubj`j'id`i'=(cdfBsubj`j'id`i'<=r(mean))
			sum cdfBsubj`j'id`i', detail
			replace LowerBoundsubj`j'id`i'=1 if cdfBsubj`j'id`i'== r(min)
            gen UpperBoundsubj`j'id`i'=(cdfBsubj`j'id`i'>r(mean)) 
			sum cdfBsubj`j'id`i', detail
			replace UpperBoundsubj`j'id`i'=1 if cdfBsubj`j'id`i'==r(max)
            gen TresholdsubLowerj`j'id`i'=(LowerBoundsubj`j'id`i'+LowerBoundsubj`j'id`i'[_n-1])
            gen TresholdsubUpperj`j'id`i'=(UpperBoundsubj`j'id`i'+UpperBoundsubj`j'id`i'[_n+1])
            sum RTordered if TresholdsubLowerj`j'id`i'==1 | TresholdsubUpperj`j'id`i'==1, detail
            replace RtpredB`i'  = r(mean) if subj==`j'  & idpair==`i'
           drop LowerBoundsubj`j'id`i' UpperBoundsubj`j'id`i' TresholdsubLowerj`j'id`i' TresholdsubUpperj`j'id`i'
        }		
local i = `i' + 1
            }			
local j = `j' + 1
            }


*Calculate predicted RTs from TWT				
*This is an out-of-sample estimation, exclude pairs used for the estimation from the comparisons
local i = 1
          while `i' < 11{	
gen RTpred`i'=   RtpredA`i' if idright==5 & idleft==`i'				  
replace RTpred`i'= RtpredA`i' if idleft==5 & idright==`i'		

gen ratio`i'=ratioA  if idleft==`i'	 &  idright==5
replace ratio`i'=ratioB  if idright==`i' &  idleft==5 
local i = `i' + 1
            }	
			
*Collapse dataset at the subject and pair level
sort subj idpair idleft	
collapse idleft idright RTpred* ratio* choiceleft, by(subj idpair)

*Create binary choices out of choice proportions
gen Choiceleft =0 if choiceleft<0.5
replace Choiceleft=1 if choiceleft>0.5	
quietly{
local i = 1
          while `i' < 11{
gen Choiceleft`i' = Choiceleft  if idleft==`i'		
replace Choiceleft`i'=Choiceleft  if idright==`i'	   
local i = `i' + 1
            }	
			}
*Create pair and subject specific variables for the predicted RTs				
local i = 1
          while `i' < 11{
egen Ratio`i'= mean(ratio`i'), by(subj)
egen RTPred`i'= mean(RTpred`i'), by(subj)
replace ratio`i'= Ratio`i'
replace RTpred`i'= RTPred`i'
local i = `i' + 1
            }	
*Drop variables used just for the loop			
drop 	Ratio* RTPred*				

*Save base prediction dataset			
save predNoTimePressureBars, replace
********************************************
* Take care of changing the name of the saved datasets between the four conditions!
********************************************

*Predictions in TWT are based on the comparisons of the predicted RTs.
use predNoTimePressureBars,clear			
gen left=.
local i = 1
          while `i' < 10{	
local j = 1
          while `j' < 10{	
		  		    *Predictions using Response times (cases 3 and 4 of Theorem 1 in the paper)
replace left = 0 if  RTpred`i'< RTpred`j' & idleft!=5 & idright!=5  
replace left = 1 if  RTpred`i'> RTpred`j' & idleft!=5 & idright!=5 

                   *Predictions using transitivity (cases 1 and 2 of Theorem 1 in the paper)	
replace left = 0 if  ratio`i'<0.5 &  ratio`j'>0.5 & idleft!=5 & idright!=5
replace left = 1 if  ratio`i'>0.5 &  ratio`j'<0.5 & idleft!=5 & idright!=5
local j = `j' + 1
            }			
local i = `i' + 1
            }


*Create the correctly-predicted variable
gen corrpredTWTNoP_Bars = 0     if   left==1  & Choiceleft==0    & left!=. & choiceleft!=.
replace corrpredTWTNoP_Bars = 0 if   left==1  & Choiceleft==0    & left!=. & choiceleft!=.
replace corrpredTWTNoP_Bars =1  if   left==1  & Choiceleft==1	 & left!=. & choiceleft!=.		
replace corrpredTWTNoP_Bars =1  if   left==0  & Choiceleft==0	 & left!=. & choiceleft!=.		
************************
*Change the name of the variable for each condition!
************************
*keep only useful variables
collapse corrpredTWTNoP_Bars nopressure Pie, by(subj)

save propcorrpredNoTimePressureBars, replace
*Adapt the program for the other 4 conditions.
*Files should be called:
*	propcorrpredNoTimePressurePie
*	propcorrpredTimePressureBars
*	propcorrpredTimePressurePie


